/**
 * 
 */
package ar.edu.utn.frsf.cidisi.ginet.generation.elementsestructure.elementstemplate;

import java.util.Map;

import org.eclipse.emf.common.util.EList;
import org.pnml.tools.epnk.pnmlcoremodel.AnnotationGraphics;
import org.pnml.tools.epnk.pnmlcoremodel.Coordinate;
import org.pnml.tools.epnk.pnmlcoremodel.Name;
import org.pnml.tools.epnk.pnmlcoremodel.PlaceNode;
import org.pnml.tools.epnk.pnmlcoremodel.PnmlcoremodelFactory;
import org.pnml.tools.epnk.pntypes.hlpng.pntd.hlpngdefinition.Declaration;

import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Arc;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Page;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Place;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.RefPlace;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Transition;
import ar.edu.utn.frsf.cidisi.ginet.generation.elementsestructure.NodeStructure;
import ar.edu.utn.frsf.cidisi.ginet.generation.elementsestructure.TreeInterface;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionPath;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;

/**
 * @author Neri
 *
 */
public class CancelUpcolbpipStructure extends ChoreographyStructure {
	
	public CancelUpcolbpipStructure(Integer i){		
		super();		
		//Name		
		AnnotationGraphics graphicsName = PnmlcoremodelFactory.eINSTANCE
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = PnmlcoremodelFactory.eINSTANCE.createCoordinate();
		
		coordinate2.setX(-9);
		coordinate2.setY( -39);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText("CANCEL" +i);
		nameLabel.setGraphics(graphicsName);
		
		this.abstractPage.setName(nameLabel);
	}
	
	@Override
	public void createPageStructure(NodeStructure node, TreeInterface tree, PlaceNode placeIn, PlaceNode placeOut, EList<Declaration> ginetDeclarations, Place termination){		
		
		this.tree = tree;
		
		this.fillVarDeclAndNameSort(ginetDeclarations);
		
		Map<InteractionPath, EList<InteractionProtocolElement>> childrenCount = node.getChildrens();
		
		boolean whitRefPlaces = placeIn != null && placeOut != null;
		
		RefPlace refPlaceIn = null; 
		Place placeCancelIn = null;
		if(whitRefPlaces){
			refPlaceIn = this.createRefPlace("InPlace",  1, 1, "InPlace", -4, -4, "1", placeIn);
		}else{
			placeCancelIn = this.createPlace("InPlace", 1, 1, "InPlace", -4, -4, this.getVarDec("gi"), 6, 21, true, "1");
		}
		
		RefPlace refPlaceOut = null; 
		Place placeCancelOut = null;
		if(whitRefPlaces){
			refPlaceOut = this.createRefPlace("OutPlace", 3, 1, "OutPlace", -4, -4, "1", placeOut);
		}else{
			placeCancelOut = createPlace("OutPlace", 3, 1, "OutPlace", -4, -4, this.getVarDec("gi"), 6, 21, false, "1");
		}
		
		if(childrenCount.size() > 0){
			Page page = createPage("page", 2, 1, "page", -9, -39);
			
			if(whitRefPlaces){
				this.createInteractionProtocol(childrenCount.keySet().iterator().next().getElement(), 0, page, refPlaceIn, refPlaceOut, ginetDeclarations, termination);
			}else{
				this.createInteractionProtocol(childrenCount.keySet().iterator().next().getElement(), 0, page, placeCancelIn, placeCancelOut, ginetDeclarations, termination);
			}
			
			this.abstractPage.getObject().add(page);
		}else{
			Transition transition2 = this.createTransition("cancel", 2, 1, "cancel", -9, -39, "1", "1", null);
			this.abstractPage.getObject().add(transition2);
			Arc arc1;
			Arc arc2;
			if(whitRefPlaces){
				arc1 = this.createArc("", this.getVarDec("gi"), -19, -6, refPlaceIn, transition2);
				arc2 = this.createArc("", this.getVarDec("gi"), -9, -9, transition2, refPlaceOut);
			}else{
				arc1 = this.createArc("", this.getVarDec("gi"), -19, -6, placeCancelIn, transition2);
				arc2 = this.createArc("", this.getVarDec("gi"), -9, -9, transition2, placeCancelOut);
			}
			this.abstractPage.getObject().add(arc1);				
			this.abstractPage.getObject().add(arc2);
		}
		
		if(childrenCount.size() > 1){
			Transition transition = this.createTransition("trigger", 2, 2, "trigger", -9, -39, "1", "1", null);
			this.abstractPage.getObject().add(transition);
			
			Place place1 = createPlace("cacthIn", 3, 2, "cacthIn", -4, -4, this.getVarDec("gi"), 6, 21, false, "1");
			this.abstractPage.getObject().add(place1);
			
			Arc arc1;
			if(whitRefPlaces){
				arc1 = this.createArc("", this.getVarDec("gi"), -19, -6, refPlaceIn, transition);
			}else{
				arc1 = this.createArc("", this.getVarDec("gi"), -19, -6, placeCancelIn, transition);
			}
			
			Arc arc2 = this.createArc("", this.getVarDec("gi"), -9, -9, transition, place1);
			this.abstractPage.getObject().add(arc1);
			this.abstractPage.getObject().add(arc2);
			
			RefPlace refplace = this.createRefPlace("cacthOut", 5, 2, "cacthOut", -4, -4, "1", termination);
			this.abstractPage.getObject().add(refplace);
			
			for(int i = 1; i < childrenCount.size(); i++){
				Page page = createPage("page" + i, 4, i+1, "page" + i, -9, -39);
				
				this.createInteractionProtocol(((InteractionPath)childrenCount.keySet().toArray()[i]).getElement(), i, page, place1, refplace, ginetDeclarations, termination);
				
				this.abstractPage.getObject().add(page);
			}
		}
		
		if(whitRefPlaces){
			this.abstractPage.getObject().add(refPlaceIn);
			this.abstractPage.getObject().add(refPlaceOut);
		}else{			
			this.abstractPage.getObject().add(placeCancelIn);
			this.abstractPage.getObject().add(placeCancelOut);
		}
	}
}